package game.client.net;

import game.common.net.GameHandler;
import game.common.world.Vector;

import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;

import javax.swing.text.Position;

import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.NonBlockingConnection;

public class GameConnection extends GameHandler {
	
	private INonBlockingConnection nbc;

	public GameConnection(String hostname, 
						  int port,
						  ConnectionListener connectionListener,
						  WorldObjectListener positionUpdateListener) throws IOException{
		
		this.nbc = new NonBlockingConnection(hostname, port);
		this.nbc.setHandler(new TestFirstDataHandler(connectionListener, positionUpdateListener));
		this.nbc.setAutoflush(false);
	}
	
	public void sendUpdatePosition(Vector position) throws BufferOverflowException, IOException{
		nbc.write(PTYPE_UPDATE_OBJECT_POSITION);
		nbc.write((short) sizeOfVector());
		writeVector(position, nbc);
		nbc.flush();
	}
}
